package com.lixin.smartclassroom.ui.home.tab2.activity

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.groupdetail
import com.lixin.smartclassroom.api.shijuanxiangqing
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.bean.WeiduBean
import com.lixin.smartclassroom.common.commonEditTextDialog
import com.lixin.smartclassroom.databinding.ActivityCourseReviewCommentBinding
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.answer
import com.lixin.smartclassroom.ui.home.adapter.weiduList
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start


/**
 * Description 任务评价
 * Author: lwc
 * Date:2023/12/9 16:37
 */
class CourseReviewCommentAct : BaseActivity<ActivityCourseReviewCommentBinding>() {
    override fun getViewBinding() = ActivityCourseReviewCommentBinding.inflate(layoutInflater)

    override fun topBarHide() = true
    override fun startPadding() = false
    private lateinit var paperList: ArrayList<DataListBean>
    private lateinit var weiduList: ArrayList<WeiduBean>
    private var id = ""
    private var xiaojieid = ""
    override fun title(): String {
        when (user_role) {
            1, 2 -> return "任务评价"
            3 -> return "任务评价"
        }
        return super.title()
    }

    override fun iniView() {
        paperList = ArrayList()
        weiduList = ArrayList()
        id = getIntentString("id")
        xiaojieid = getIntentString("xiaojieid")
        binding.apply {
            btnContinue.setRoundRectBg(Color.parseColor("#4B6AEF"), dp2px(27f).toInt())
            tvQuestionNum.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(11f).toInt())
            tvTeacherComment.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(10f).toInt())
            llTop.setRoundRectBg(Color.parseColor("#EAEEFF"), dp2px(10f).toInt())
            llQuestionType.setRoundRectBg(Color.parseColor("#EAEEFF"), dp2px(10f).toInt())
            rcvWeidu.setRoundRectBg(Color.parseColor("#EAEEFF"), dp2px(10f).toInt())
            circleProgress.setMax(100)
            circleProgress.setCurrent(80)
            val content = "老师点评：" + "学习从来无捷径,循序渐进登高峰。加油哦!"
            // 创建一个可变的 SpannableString 对象
            val spannableString = SpannableString(content)
            // 将字符串中的后11个字符设置为蓝色
            var blueSpan = ForegroundColorSpan(Color.parseColor("#009B93"));
            spannableString.setSpan(blueSpan, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvTeacherComment.text = spannableString
        }

        binding.recyclerAnswer.answer().apply {
            R.id.tvQuestionNo.onClick {
                start(LookAnswerAct::class.java) {
                    putExtra("paperList", paperList)
                    putExtra("currentPosition", modelPosition)
                    putExtra("id", id)
                    putExtra("xiaojieid", xiaojieid)
                }
            }
        }.models = paperList

        getData()

        receiveEvent<String>("refreshData") {
            getData()
        }

    }

    private fun getData() {
        scopeNetLife {
            val baseBean = groupdetail(id)
            if (baseBean.result == "0") {
                setDetailView(baseBean)
                paperList.clear()
                paperList.addAll(shijuanxiangqing(xiaojieid, id)!!)
                binding.recyclerAnswer.bindingAdapter.notifyDataSetChanged()

                weiduList.addAll(baseBean.weidulist!!)
                binding.rcvWeidu.weiduList().models = weiduList
            }


        }
    }

    private fun setDetailView(baseBean: BaseBean) {
        binding.apply {
            tvTitle.text = baseBean.name
            tvTime.text = baseBean.createdate
            tvClass.text = "所属班级：" + baseBean.classname
            tvTotalScore.text = "总分" + baseBean.totalscouce
            tvCyrs.text = baseBean.cyrenshu + "/" + baseBean.totalclass
            tvScore.text = baseBean.pingjun
            tvContent.gone()
            tvRank.text = baseBean.paiming
            tvTeacherComment.text = baseBean.dianping
            when (baseBean.state) {
                "1" -> tvReviewState.text = "待批阅"
                "2" -> tvReviewState.text = "批阅中"
                "3" -> tvReviewState.text = baseBean.scouce
            }

        }
    }


    override fun onClick() {
        binding.apply {
            ivBack.onClick {
                finish()
            }
            btnContinue.onClick {
                finish()
            }

            btnGoReview.onClick {
                start(LookAnswerAct::class.java) {
                    putExtra("paperList", paperList)
                    putExtra("currentPosition", "0")
                    putExtra("id", id)
                    putExtra("xiaojieid", xiaojieid)
                }
            }

            tvTeacherComment.onClick {
                commonEditTextDialog(
                    2,
                    id,
                    "编辑评语",
                    "请输入评语",
                    "取消",
                    "确定",
                    textview = tvTeacherComment,

                )
            }
        }
    }

}